<template>
  <div class="game">
    <img
      class="flyBg"
      id="flyBg"
      src="@/assets/images/fly2.png"
      alt=""
      :style="{ bottom: offsetBottom + 'px' }"
    />
    <hostileVue ref="hostileVue" />
    <overlayVue ref="overlayVue" v-if="isShowOverlay" />
    <div class="top">
      <div class="left box">
        <div class="title">操作测评分数：</div>
        <div class="item" v-for="(item, index) in offsetData" :key="index">
          <div class="title">{{ item.title }}:</div>
          <div class="data">{{ planeSocer[item.key] }}</div>
        </div>
      </div>
      <div class="time backgroundImage">
        <div class="timeIcon backgroundImage"></div>
        <Countdown
          :time="20"
          format="mm:ss"
          :switch="!isShowOverlay"
          @on-end="onCountdownEnd"
          style="color: #fff; margin: 0 10px"
        >
          <template slot-scope="{ time }">{{ time }}</template>
        </Countdown>
      </div>
      <div class="right"></div>
    </div>
    <!-- <div class="speed">
      <SpeedGaugeVue />
    </div> -->
    <div class="speed">
      <speedGuendVue ref="speedGuendVue" />
      <troubdVue ref="troubdVue" />
    </div>
    <div class="bgBottom"></div>
  </div>
</template>

<script>
import Countdown from "@choujiaojiao/vue2-countdown";
import speedGuendVue from "@/components/modelComponents/speedGuend.vue";
import troubdVue from "@/components/modelComponents/troubd.vue";
import overlayVue from "@/components/overlay/overlay.vue";
import { mapGetters } from "vuex";
import { addFlyTask } from "@/api/flyGame";
import pubsub from "pubsub-js";
import hostileVue from "./hostile.vue";
import obs from "@/utils/obs.js";
export default {
  components: {
    Countdown,
    speedGuendVue,
    troubdVue,
    hostileVue,
    overlayVue,
  },
  data() {
    return {
      offsetBottom: 0, // 初始化 offset 值
      interval: null,
      isShowOverlay: true,
      offsetData: [
        {
          title: "爱心奖励数",
          key: "score",
        },
        {
          title: "碰撞飞机数",
          key: "crash",
        },
        {
          title: "消灭飞机数",
          key: "strike",
        },
        {
          title: "敌机生成数",
          key: "enemys",
        },
      ],
      pid: "",
    };
  },
  computed: {
    ...mapGetters(["planeSocer", "getWebSocket"]),
  },
  mounted() {
    // 使用 setInterval 来定时执行背景图移动的函数
    // this.moveBackground();
    this.pid = pubsub.subscribe("action2", () => {
      this.$refs.overlayVue.startCountdown();
    });
    this.interval = setInterval(this.moveBackground, 10); // 每 100 毫秒执行一次移动
  },
  methods: {
    moveBackground() {
      // 获取背景图元素的 DOMRect 对象
      let flyBg = document.querySelector("#flyBg").getBoundingClientRect();
      let flyTop = flyBg.top;
      this.offsetBottom = this.offsetBottom - this.$store.getters.bgSpeed;
      if (flyTop > -4240) {
        this.offsetBottom = 0;
      }
    },
    onCountdownEnd() {
      this.$refs.hostileVue.closeTime();
      this.$refs.speedGuendVue.cancleInterVal();
      this.$refs.troubdVue.cancleInterVal();

      this.getWebSocket.send(JSON.stringify({ status: "finish" }));
      setTimeout(() => {
        this.$router.push("/test/report");
      }, 500);
      obs.obsCall("StopRecord");
    },
    action() {
      this.isShowOverlay = false;
      this.$refs.hostileVue.actionGame();
      this.$refs.speedGuendVue.actionGamepe();
      this.$refs.troubdVue.actionGamepe();

      this.getWebSocket.send(JSON.stringify({ status: "start" }));
      obs.obsCall("StartRecord");
      addFlyTask({
        userId: localStorage.getItem("testUserId"),
        parentId: localStorage.getItem("testTaskId"),
        taskId: localStorage.getItem("game"),
      }).then((res) => {
        localStorage.setItem("gameTaskId", res.data);
      });
    },
  },
  beforeDestroy() {
    pubsub.unsubscribe(this.pid);
    clearInterval(this.interval);
  },
};
</script>

<style lang="scss" scoped>
.game {
  width: 100%;
  height: 100%;
  overflow: hidden; /* 隐藏超出部分 */
  position: relative;
  .bgBottom {
    width: 100%;
    height: 160px;
    position: absolute;
    z-index: 999;
    bottom: -10px;
    border-image-source: url("../../../../assets/images/bgBottom.png");
    border-image-slice: 100 fill; /* 设置图像边框的分割方式 */
    border-image-width: 20px; /* 设置边框宽度 */
    border-image-outset: 10; /* 设置边框扩展区域的大小 */
    border-image-repeat: stretch; /* 设置图像边框的重复方式 */
  }

  .backgroundImage {
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
  }
  .top {
    width: 100%;
    position: relative;
    z-index: 9;
    padding: 10px;
    box-sizing: border-box;
    display: flex;
    flex-flow: row;
    justify-content: space-between;
    align-items: flex-start;
    .box {
      background: #000000;
      border-radius: 12px;
      box-shadow: inset 2px 2px 12px 2px rgba(31, 114, 148, 0.3);
      opacity: 0.7;
    }
    .left {
      width: 260px;
      height: 180px;
      display: flex;
      flex-flow: column;
      justify-content: center;
      align-items: center;
      gap: 10px;
      .title {
        width: 80%;
        text-align: left;
        color: #fff;
        font-weight: 400;
        font-size: 16px;
        color: #b2bec2;
      }
      .item {
        width: 100%;
        display: flex;
        flex-flow: row;
        justify-content: center;
        align-items: center;
        gap: 10px;
        padding-left: 20%;
        box-sizing: border-box;
        .title {
          width: 50%;
          text-align: right;
          color: #fff;
          font-weight: 400;
          font-size: 16px;
          color: #b2bec2;
        }
        .data {
          width: 50%;
          text-align: left;
          color: #fff;
          font-weight: bold;
          font-size: 16px;
          color: #f14024;
        }
      }
    }

    .time {
      width: 180px;
      height: 60px;
      background-image: url("../../../../assets/images/timeBg.png");
      background-size: cover;
      padding: 10px 15px;
      box-sizing: border-box;
      display: flex;
      flex-flow: row;
      align-items: center;
      justify-content: center;
      .timeIcon {
        width: 30px;
        height: 30px;
        background-image: url("../../../../assets/images/time.png");
      }
    }
    .right {
      width: 288px;
      height: 162px;
      background: #000000;
      box-shadow: 0px 0px 8px 0px rgba(4, 22, 29, 0.4),
        0px 0px 16px 0px rgba(174, 182, 185, 0.4);
      border-radius: 12px;
      opacity: 0;
    }
  }
  .speed {
    position: relative;
    z-index: 9;
    display: flex;
    flex-flow: row;
    justify-content: space-between;
  }
  .flyCanvas {
    position: absolute;
    top: 0;
    left: 0;
    z-index: 99999; /* 确保 canvas 处于 img 下面 */
    width: 100%;
    height: 1000%; /* 背景图大小与容器大小相同 */
  }
  .flyBg {
    width: 100%;
    height: 1000%; /* 背景图大小与容器大小相同 */
    position: absolute;
    background-repeat: repeat-y; /* 仅垂直方向平铺 */
  }
}
</style>
